# -*- coding: utf-8 -*-
#===============================================================================
#  ns tracefile parsing
#===============================================================================
from process import process
from process import inf
 


import os



# file
print "starting..."
f = open("../simple.tr","r")
nodenum = 8
def r1(self):
    return self.node == 7
def s1 (self):
    return self.node == 0
inf.recieved = r1
inf.sent = s1
proc = process(nodenum)
proc.open()
proc.parse(f)
proc.close()
print "done stats..."
print "bulding plots..."

drf = open("../data/dropreason",'w')
for reason, count in proc.pack_stats .iteritems():
    drf.write(reason+" "+`count`+"\n")
drf.close();
    


gphs = """set style line 1 lt rgb "green"
set style line 2 lt rgb "black"
set style line 3 lt rgb "blue"
set style line 4 lt rgb "red"
set terminal png size 800 600
"""

for i in range(nodenum):
    feedme = os.popen("gnuplot", "w")
    feedme.write(gphs)
    si = `i`
    fi = "../data/"+`i`
    feedme.write("set output '../graphs/"+si+".png'\n")
    feedme.write("plot '"+fi+"' using 1:2 ls 1 title 'send rate' with lines , '"+fi+"' using 1:3 ls 2 title 'recv rate' with lines , '"+fi+"' using 1:4 ls 3 title 'fwrd rate' with lines , '"+fi+"' using 1:5 ls 4 title 'lost rate' with lines \n")
    feedme.close()

print "done per-node plots"
feedme = os.popen("gnuplot", "w")
feedme.write("""set terminal png size 800 600
set output '../graphs/overall.png'
plot '../data/overall' using 1:2 title 'sent' with lines, '../data/overall' using 1:3 title 'recvd' with lines 
""")
feedme.close()


feedme = os.popen("gnuplot", "w")
feedme.write("""set terminal png size 800 600
set output '../graphs/overall-ping.png'
plot '../data/overall' using 1:4 title 'ping_one' with lines, '../data/overall' using 1:5 title 'ping_two' with lines 
""")
feedme.close()

print "done plots"
    


#for pack in packages.itervalues():
#    del pack[0:2]
#    start = pack[0]
#    end = pack[-1]
#    for p  in pack:
#        p.start = start
#        p.end = end
#    
#
## print history of single package  
#for id, pack in packages.iteritems():
#    timespend = pack[-1].time - pack[0].time
#    tp = pack[-1].event_type+`pack[-1].node`
#    if tp in results.keys():
#        results[tp].append(pack)
#    else:
#        results[tp] = [pack]
##    print id ,pack[0].type,timespend,":", 
#    
##    for package_event in pack :
##        print package_event.event_type, package_event.node,"@",package_event.time,"|",
##    print 
#
#for id, amount in results.iteritems():
#    print id, len(amount)
#
#print
#
## time period for stats 
#dct=0.3
#
## print load be node
#for i,nodestat in zip(range(0,len(nodes)), nodes):
#    print i
#    ct=dct
#    
#    csv = open(`i`+".csv","w")
#    # amount of sent, received, forwarded, and dropped bytes
#    send,recv,fwd,drp=0,0,0,0
#    for node_event in nodestat:
#        # print stats for next period 
#        if node_event.time > ct :
#            print '@ ct',ct,'\tsend:' , send*8/(dct*1000), '\trecv:', recv*8/(dct*1000),'\tfwd:',fwd*8/(dct*1000),'\tdrop:',drp*8/(dct*1000)
#            csv.write(`ct`+"\t"+`send*8/(dct*1000)`+"\t"+`recv*8/(dct*1000)`+"\t"+`fwd*8/(dct*1000)`+"\t"+`drp*8/(dct*1000)`+"\n")
#            send,recv,fwd,drp=0,0,0,0
#            ct+= dct
#        if node_event.event_type == 's' : 
#            send += node_event.len
#        elif node_event.event_type == 'r' :
#            recv += node_event.len
#        elif node_event.event_type == 'f' :
#            fwd += node_event.len
#        elif node_event.event_type == 'd' :
#            drp += node_event.len
#    csv.close()
#
#    
    


